<?php
namespace Home\Controller;
use  Think\Controller;

class PersonalController extends CommonController{
    // 进入个人中心页面
    public function user(){

        $id = session(C(SESSIONUSER));
        $res_user = M('user')->where('id = "'.$id.'"')->find();

        $level = $res_user['level'];
        if($level == 1){
            $res_user['level'] = '普通会员';
        }else if($level == 2){
            $res_user['level'] = 'vip会员';
        }else if($level == 3){
            $res_user['level'] = '天使合伙人';
        }else{
            $res_user['level'] = '梦想合伙人';
        }
        $table1 = 'detail'.$level;
        $table2 = 'pic'.$level;

        $res_detail = M($table1)->where("id = '".$res_user['detailid']."'")->find();
        $res_pic = M($table2)->where("id = '".$res_user['picid']."'")->find();

        if($res_user['pid'] != 0){

            $res2 = M('user')->where('id ="'.$res_user['pid'].'"')->find();
            $res_user['parent'] = $res2['user'];
        }else{
            $res_user['parent'] = '无';
        }

        $res_user['phone'] = $res_detail['phone'];
        $res_user['addres'] = $res_detail['addres'];

        $res_num = M('cutnum')->group('userId')->field('sum(num) as number,userId')->order('number')->select();

        foreach($res_num as $k=>$v){

            if($id == $v['userid']){
                $res_user['num'] = $v['number'];
                $res_user['rank'] = $k+1;
            }
        }

        $where['num'] = array('LT','0');
        $where['userId'] = array('eq',$id);

        $res3 = M('cutnum')->where($where)->field('sum(num) as number')->select();
        $res4 = M('cutnum')->where('userId = "'.$id.'"')->field('sum(num) as number')->select();

        $res_user['cnum'] = abs($res3[0]['number'])+ abs($res4[0]['number']);
        $res_user['xnum'] = abs($res3[0]['number']);// 已用名额
        $res_user['snum'] = abs($res4[0]['number']);// 剩余名额



        $this->assign('list',$res_user);

        $this->display();

    }
    // 生成邀请码
    public function sendcode(){

        $userId = session(C(SESSIONUSER));

//        var_dump($_SESSION['user']['userId']);die;
        $res_user = M('user')->where('id = "'.$userId.'"')->find();

        if($res_user['level'] == 1){
            $data = genBody('2','只有VIP才能邀请');
            $this->ajaxReturn($data,'JSON');
        }

        if(!$res_user['code']){
            $code = $this->createcode($userId);
            // 将生成的code 插入到user表中
            $arr_user['code'] = $code;
            $res_user = M('user')->where("id = '".$userId."'")->save($arr_user);

            if($res_user){
                $data = genBody('1','获取成功');
                $this->ajaxReturn($data,'JSON');
            }

        }else{
            $code  = $res_user['code'];

            if($code){
                $data = genBody('3','您已有邀请码');
                $this->ajaxReturn($data,'JSON');
            }

        }

    }


    public function createcode($userId){

        $str = 'abcdefghigklmnopqrstuvwxyz1234567890';

        $str = str_shuffle($str);

        $code = strtoupper(substr($str,0,10).$userId);

        $arr['code'] = $code;
        $res = M('user')->where('id = "'.$userId.'"')->save($arr);

        if($res){

            $_SESSION['user']['code'] = $code;
        }

        return $code;
    }

    public function nowedit(){

        $name = I('name');
        $value = I('value');
        $id = session(C(SESSIONUSER));
//        $data = genBody('2','测试');
//        $this->ajaxReturn($data,'JSON');
        if($name == 'psw'){
            $arr_user['password'] = $value;
            $res_user1 = M('user')->where('id = "'.$id.'"')->save($arr_user);// 修改
            if($res_user1){
               
               $data = genBody('1','修改成功');
               $this->assign($data,'JSON');        
            }else{
                
               $data = genBody('0','修改失败');
               $this->assign($data,'JSON');        
            }
            
            
        }else{
            $arr_user['addres'] = $value;
            $user1 = M('user')->where('id = "'.$id.'"')->find();
            
            $table = 'detail'.$user1['level'];
            $res_detail = M($table)->where('id = "'.$user1['detailid'].'"')->save($arr_user);
            
            if($res_detail){
                $data = genBody('1','修改成功');
                
                $this->ajaxReturn($data,'JSON');
                        
            }else{
                $data = genBody('0','修改失败');
                
                $this->ajaxReturn($data,'JSON'); 
                
            }
            
        }
        
        
    }

    // 展示邀请信息
    public function showinvite(){

        $id = session(C(SESSIONUSER));

        $res_invite = M('invite')->where("object = '".$id."'")->order('endTime desc')->select();

        $this->assign('data',$res_invite);
        $this->display();

    }
    
    // 接收邀请
    // pid  邀请这ID
    public function receive(){

        $pid = I('userId');  // 接收邀请的邀请者ID;
        $id = session(C(SESSIONUSER));  // 接受者ID
        $res_status = M('status')->where("userId = '".$id."' and status = 2 and state = 1")->find();

        $res_user = M('user')->where("id = '".$id."'")->find(); 

        if($res_status['level'] == 3){
            
            if($res_user['level'] == $res_status['level']){
                $num = 20;
                $this_int = 1;// 代表重复购买
            }else{
                $num = 19;
                $this_int = 2;// 代表非重复购买
            }
            // 既当该用户重复购买天使会员权限时
    
            $level = $res_status['level']; // 需要的等级
            $res_num = M('cutnum')->where("userId = '".$pid."'")->field('sum(num) as number')->select();

            if($res_num[0]['number'] < $num){

                $data = genBody('1','该用户名额不足，请更换');
                $this->ajaxReturn($data,'JSON');
            }else{

                // TODO  ::  邀请者减去相应的名额  获得相应的收入
                 $getmoney = M('system1')->where('id = 1')->find();
                 $money = $getmoney['first'];
                 $userId = $pid;
                 $geterId = $id;

                 $res_num = $this->get_cutnum($num,$userId,$geterId);
                 if($res_num){

                     $res_money = $this->get_cutmoney($money,$userId,$geterId,$this_int,$level);
                 }

                if($res_money){
                    // 金额关系已经修改  user pid 已经修改  status表 state未修改
                    $arr_status['state'] = 2;  // 已经找到新的上级
                    $result_status = M('status')->where("id = '".$res_status['id']."'")->save($arr_status);
                }
            
            }
        }else if($res_status['level'] == 2){

            if($res_user['level'] == $res_status['level']){
                
                $data = genBody('2','VIP不能重复购买');

            }else{

                $num = 1;
            }
            
            $this_int = 2 ; // 非重复购买
            $level = $res_status['level']; // 需要的等级
            $res_num = M('cutnum')->where("userId = '".$pid."'")->field('sum(num) as number')->select();

            if($res_num[0]['number'] < $num){

                $data = genBody('1','该用户名额不足，请更换');
            }else{

                // TODO  ::  邀请者减去相应的名额  获得相应的收入
                 $getmoney = M('system1')->where('id = 1')->find();
                 $money = $getmoney['vip'];
                 $userId = $pid;
                 $geterId = $id;
                 $res_num = $this->get_cutnum($num,$userId,$geterId);

                 if($res_num){
                     $res_money = $this->get_cutmoney($userId,$geterId,$this_int,$level);
                 }

                 if($res_money){
                     // 金额关系已经修改  user pid 已经修改  status表 state未修改
                     $arr_status['state'] = 2;  // 已经找到新的上级
                     $result_status = M('status')->where("id = '".$res_status['id']."'")->save($arr_status);
                 }
            
            }
        }
    }

    // 金额计算
    // $money  金额
    // userId  获得者ID
    // geterId  支付者ID
    // this_int 重复购买的标志

    // 在平台接受邀请后  原来的付款记录更改金钱付款记录
    public function  get_cutmoney($money,$userId,$geterId,$this_int,$level){

         if($this_int == 1){
            $dbtable = 'repeartmoney';  // 重复购买记录表
         }else{
            $dbtable = 'updatemoney';  // 升级购买记录表
         }
         // 将未成功的合伙人转为当前接受邀请的合伙人
         $where1['geterId'] = $geterId;
         $where1['level'] = $level;
         $where1['state'] = 2;

        $where2['userId'] = $geterId;
        $where2['level'] = $level;
        $where2['state'] = 2;

        $res_money1 = M($dbtable)->where($where1)->find();
        $res_money2 = M($dbtable)->where($where2)->find();

         if(!$res_money1 || !$res_money2){

             $data = genBody('1','参数错误');
             $this->ajaxReturn($data,'JSON');
         }

         $arr_money1['userId'] = $userId;
         $arr_money1['state'] = 1;
         $arr_money1['time'] = time();


         $arr_money2['geterId'] = $userId;
         $arr_money2['state'] = 1;
         $arr_money2['time'] = time();

         M($dbtable)->startTrans();  // 开启事务

         $res1 = M($dbtable)->where("id = '".$res_money1['id']."'")->save($arr_money1);
         $res2 = M($dbtable)->where("id = '".$res_money2['id']."'")->save($arr_money2);

         if($res1 && $res2){

            M($dbtable)->commit();
         }else{

            M($dbtable)->rollback();
         }

         return 1;

    }

    
    // 接受邀请付款成功后  给予名额
    public function get_cutnum($num,$userId,$geterId){

        M('cutnum')->startTrans();

        $arr_num1['userId'] = $userId;
        $arr_num1['geterId'] = $geterId;
        $arr_num1['num'] = -$num;

        $res_num1 = M('cutnum')->add($arr_num1);

        $arr_num2['userId'] = $geterId;
        $arr_num2['geterId'] = $userId;
        $arr_num2['num'] = $num;

        $res_num2 = M('cutnum')->add($arr_num2);

        if($res_num1 && $res_num2){

            M('cutnum')->commit();
        }else{
           
           M('cutnum')->rollback();
        }

        $arr_user['pid'] = $userId;
        $res_user_pid = M('user')->where("id ='".$geterId."'")->save($arr_user);

        if($res_user_pid){

            return 1;
        }
    }

}